home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / cuj9205.zip / 1005016A < prev    next >
Text File  |  1992-06-02  |  639b  |  33 lines

  1.  
  2. Listing 3 -- strtol.c
  3.  
  4.  
  5. /* strtol function */
  6. #include <ctype.h>
  7. #include <errno.h>
  8. #include <limits.h>
  9. #include <stdlib.h>
  10.  
  11. long (strtol)(const char *s, char **endptr, int base)
  12.     {    /* convert string to long, with checking */
  13.     const char *sc;
  14.     unsigned long x;
  15.  
  16.     for (sc = s; isspace(*sc); ++sc)
  17.         ;
  18.     x = _Stoul(s, endptr, base);    /* not sc! */
  19.     if (*sc == '-' && x <= LONG_MAX)
  20.         {    /* negative number overflowed */
  21.         errno = ERANGE;
  22.         return (LONG_MIN);
  23.         }
  24.     else if (*sc != '-' && LONG_MAX < x)
  25.         {    /* positive number overflowed */
  26.         errno = ERANGE;
  27.         return (LONG_MAX);
  28.         }
  29.     else
  30.         return ((long)x);
  31.     }
  32.  
  33.